package org.fing.edu.uy.esbadp.modules.persistance;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;

import org.apache.commons.io.FileUtils;
import org.fing.edu.uy.esbadp.constants.EsbAdpPropertyConfig;
import org.fing.edu.uy.esbadp.core.entity.DMRegisteredService;
import org.fing.edu.uy.esbadp.core.entity.KeyValuePair;
import org.fing.edu.uy.esbadp.core.entity.MetaDataConfig;
import org.fing.edu.uy.esbadp.core.registry.RegistryManager;
import org.fing.edu.uy.esbadp.logger.LoggerADP;
import org.fing.edu.uy.esbadp.utils.PathEvaluate;
import org.jboss.soa.esb.persistence.manager.ConnectionManager;
import org.jboss.soa.esb.persistence.manager.ConnectionManagerException;
import org.jboss.soa.esb.persistence.manager.ConnectionManagerFactory;

/**
 * 
 * @author dev
 * 
 *         Implementacion para para acceder a los servicios y las propiedades
 *         registradas.
 * 
 */
public class ServiceManagerImpl implements ServiceManagerInterface {

	public static final String DEFAULT_VIRTUAL_SERVICE_TABLE_NAME = "SERVICE";
	public static final String DEFAULT_VIRTUAL_SERVICE_SEQ_TABLE_NAME = "SERVICE_SEQ";
	public static final String DEFAULT_VIRTUAL_SERVICE_METADATA_TABLE_NAME = "METADATA";
	public static final String DEFAULT_VIRTUAL_SERVICE_EQUIV_TABLE_NAME = "SERVICE_EQUIV";
	public static final String DEFAULT_FROM_CANONICAL_SUFFIX = "FROM.xsl";
	public static final String DEFAULT_TO_CANONICAL_SUFFIX = "TO.xsl";
	public static final String DEFAULT_CANONICAL_FOLDER = "/esbadp/xslStore";
	
	
			
	/***********************************************************************************************************************/
	/***********************************CONSULTAS UTILIZADAS PARA PERSISTIR LOS SERVICIOS **********************************/
	/***********************************************************************************************************************/
	
	private static final String INSERT_SERVICE = "INSERT INTO %1 (SERVICE_ID, NAME, CATEGORY_NAME, ENDPOINT, ESB_PROYECT) values(?,?,?,?,?)";
	private static final String GET_NEXT_SEQ_VALUE = "SELECT NEXT VALUE FOR %1 AS NEXTSEQ FROM DUAL";	
	private static final String INSERT_METADATA = "INSERT INTO %1 (META_KEY, META_VALUE, SERVICE_ID) values(?,?,?)";
	private static final String SELECT_SERVICE = "SELECT SERVICE_ID, NAME, CATEGORY_NAME, ENDPOINT, ESB_PROYECT FROM %1 WHERE SERVICE_ID = ?";	
	private static final String SELECT_SERVICE_FROM_ENDPOINT = "SELECT SERVICE_ID, NAME, CATEGORY_NAME, ENDPOINT, ESB_PROYECT FROM %1 WHERE LCASE(ENDPOINT) = ?";
	private static final String SELECT_METADATA = "SELECT META_KEY, META_VALUE, SERVICE_ID FROM %1 WHERE SERVICE_ID = ?";
	private static final String SELECT_ALL_SERVICES = "SELECT SERVICE_ID, NAME, CATEGORY_NAME, ENDPOINT, ESB_PROYECT FROM %1";
	private static final String UPDATE_SERVICE = "UPDATE %1 SET NAME = ?, CATEGORY_NAME = ?, ENDPOINT = ?, ESB_PROYECT = ? WHERE SERVICE_ID = ? ";
	private static final String UPDATE_METADATA = "UPDATE %1 SET META_VALUE = ? WHERE SERVICE_ID = ? AND META_KEY = ?";
	private static final String DELETE_SERVICE = "DELETE FROM %1 WHERE SERVICE_ID = ? ";
	private static final String DELETE_METADATA = "DELETE FROM %1 WHERE META_KEY = ? AND SERVICE_ID = ?";
	
	private static final String INSERT_EQUIV = "INSERT INTO %1 (SERVICE_ID,EQUIV_ID) VALUES (?,?)";
	private static final String SELECT_EQUIV_SERVICE = "SELECT SERVICE_ID,EQUIV_ID FROM %1 WHERE SERVICE_ID = ?";
	private static final String DELETE_EQUIV_ALL = "DELETE FROM %1 WHERE SERVICE_ID = ?";
	
	
	/***********************************************************************************************************************/	
	/***********************************************************************************************************************/
	
	
	protected ConnectionManager mgr = null;

	private String virtualServicesTableName = "";
	private String virtualServicesSeqTableName = "";
	private String virtualServicesMetadataTableName = "";
	private String virtualServicesEquivTableName = "";
	private String xslFolder = "";
	

	private static final java.util.logging.Logger logger = LoggerADP.getLogger(ServiceManagerImpl.class.getSimpleName());

	public ServiceManagerImpl(){

		try {
			mgr = ConnectionManagerFactory.getConnectionManager();
			virtualServicesTableName = RegistryManager.getInstance()
					.getAdpConfigProperty(
							EsbAdpPropertyConfig.virtualServiceDBTable,
							DEFAULT_VIRTUAL_SERVICE_TABLE_NAME);
			
			virtualServicesSeqTableName = RegistryManager.getInstance()
					.getAdpConfigProperty(
							EsbAdpPropertyConfig.virtualServiceSeqDBTable,
							DEFAULT_VIRTUAL_SERVICE_SEQ_TABLE_NAME);
			
			virtualServicesMetadataTableName = RegistryManager.getInstance()
					.getAdpConfigProperty(
							EsbAdpPropertyConfig.virtualServiceMetadataDBTable,
							DEFAULT_VIRTUAL_SERVICE_METADATA_TABLE_NAME);
			
			virtualServicesEquivTableName = RegistryManager.getInstance()
					.getAdpConfigProperty(
							EsbAdpPropertyConfig.virtualServiceEquivDBTable,
							DEFAULT_VIRTUAL_SERVICE_EQUIV_TABLE_NAME);
			
			xslFolder =  PathEvaluate.evaluatePath(RegistryManager.getInstance().getAdpConfigProperty(EsbAdpPropertyConfig.pathXSLTStore, DEFAULT_CANONICAL_FOLDER));
			
		} catch (ConnectionManagerException e) {			
			logger.log(Level.SEVERE,"Error creating new instance ->" , e);
			
		}
	   
	}
	
	
	

	/**
	 * add's a @DMRegisteredService to the database persistence store 
	 * and their @MetaDataConfig asociated metadata. 
	 */
	@Override
	public int persistVirtualService(DMRegisteredService virtualService) throws SQLException{

		Connection conn = null;
		PreparedStatement ps = null;
		
		Integer serviceID = getNextSeqValue();		
		
		try {
			conn = mgr.getConnection();
			String sql = INSERT_SERVICE.replace("%1", virtualServicesTableName);		
			ps = conn.prepareStatement(sql);			
			ps.setInt(1,serviceID);
			ps.setString(2, virtualService.getVirtualServiceName());
			ps.setString(3, virtualService.getVirtualServiceCategory());
			ps.setString(4, virtualService.getEndPointURL());
			ps.setString(5, virtualService.getEsbProyectName());
			ps.execute();
			
			//persisto la metadata
			if(serviceID!=-1){		
				sql  = INSERT_METADATA.replace("%1", virtualServicesMetadataTableName);
			    ps = conn.prepareStatement(sql);				
				for(KeyValuePair aux : virtualService.getMetadata().attributesAsList()){					
					ps.setString(1, aux.getKey());
					ps.setString(2, aux.getValue());
					ps.setInt(3, serviceID );					
					ps.execute();
				}
			}			
		} catch (SQLException e) {			
			logger.log(Level.SEVERE,"Persisting virtual service failed with exeception ->" , e);
			throw e;
			
		} finally {			
			release(conn,ps);
		}
		
		return serviceID;
	}
	
	/**
	 * 
	 * @param serviceID identificador del servicio al cual se le asociara la metadata
	 * @param meta es la metadata a persistir para el servicio
	 */
	public void persistVirtualServiceMetadata(MetaDataConfig meta , long serviceID) throws SQLException{
		
		Connection conn = null;
		PreparedStatement ps = null;

		try {
			conn = mgr.getConnection();
			String sql  = INSERT_METADATA.replace("%1", virtualServicesMetadataTableName);
		    ps = conn.prepareStatement(sql);				
			for(KeyValuePair aux : meta.attributesAsList()){					
				ps.setString(1, aux.getKey());
				ps.setString(2, aux.getValue());
				ps.setLong(3,serviceID );					
				ps.execute();
			}
		} catch (SQLException e) {			
			logger.log(Level.SEVERE,"Persisting virtual service metadata with exeception ->" , e);
			throw e;			
		} finally {			
			release(conn,ps);
		}		
	}
		
	
	
	/**
	 * 
	 * @param serviceID id del servicio que se quiere levantar de la BD
	 * @return El servicio que correnponde con serviceID o null si este no existe.
	 */
	@Override
	public DMRegisteredService getVirtualService(long serviceID) throws SQLException{

		DMRegisteredService ret  = null;
		
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			conn = mgr.getConnection();
			String sql = SELECT_SERVICE.replace("%1", virtualServicesTableName);
		  	ps = conn.prepareStatement(sql);		
			ps.setFloat(1, serviceID);		
			ResultSet res = ps.executeQuery();
			if(res.next()){
				String name = res.getString("NAME");
				String category_name = res.getString("CATEGORY_NAME");
				String endpoint = res.getString("ENDPOINT");
				String esb_proyect = res.getString("ESB_PROYECT");				
				
				ret = new DMRegisteredService();
				ret.setEndPointURL(endpoint);
				ret.setEsbProyectName(esb_proyect);
				ret.setServiceID(serviceID);
				ret.setVirtualServiceCategory(category_name);
				ret.setVirtualServiceName(name);
				ret.setCanonicalXSL(this.hasCanonicalXSL(serviceID));
				
				//cargo la metadata asociadas
				MetaDataConfig meta = getVirtualServiceMetadata(serviceID);
				ret.setMetadata(meta);			
			}
			
		} catch (SQLException e) {			
			logger.log(Level.SEVERE,"Getting virtual service failed with exeception ->" , e);		
			throw e;
		} finally {			
			release(conn,ps);
		}
		
		return ret;
	}
	
	/**
	 * 
	 * @param endpoint url  del servicio que se quiere levantar de la BD
	 * @return El servicio que correnponde con endpoint o null si este no existe.
	 */
	@Override
	public DMRegisteredService getVirtualService(String endpointUrl) throws SQLException {
		DMRegisteredService ret  = null;
		
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			conn = mgr.getConnection();
			String sql = SELECT_SERVICE_FROM_ENDPOINT.replace("%1", virtualServicesTableName);
		  	ps = conn.prepareStatement(sql);		
			ps.setString(1, endpointUrl);		
			ResultSet res = ps.executeQuery();
			if(res.next()){
				String name = res.getString("NAME");
				String category_name = res.getString("CATEGORY_NAME");				
				String esb_proyect = res.getString("ESB_PROYECT");
				long serviceID = res.getLong("SERVICE_ID");
				
				ret = new DMRegisteredService();
				ret.setEndPointURL(endpointUrl);
				ret.setEsbProyectName(esb_proyect);
				ret.setServiceID(serviceID);
				ret.setVirtualServiceCategory(category_name);
				ret.setVirtualServiceName(name);
				ret.setCanonicalXSL(this.hasCanonicalXSL(serviceID));
				
				//cargo la metadata asociadas
				MetaDataConfig meta = getVirtualServiceMetadata(serviceID);
				ret.setMetadata(meta);			
			}
			
		} catch (SQLException e) {			
			logger.log(Level.SEVERE,"Getting virtual from endpoint service failed with exeception ->" , e);
			throw e;
		} finally {			
			release(conn,ps);
		}
		
		return ret;
	}
	
	
	/**
	 * 
	 * @param serviceID identificador del servicio que contiene la metadata buscada
	 * @return toda la metadata asociada al servicio que tiene como identificador el parametro
	 */
	@Override
	public MetaDataConfig getVirtualServiceMetadata(long serviceID) throws SQLException {
		MetaDataConfig ret  = new MetaDataConfig();
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			conn = mgr.getConnection();
			String sql = SELECT_METADATA.replace("%1", virtualServicesMetadataTableName);
		  	ps = conn.prepareStatement(sql);		
			ps.setFloat(1, serviceID);		
			ResultSet res = ps.executeQuery();
			while(res.next()){				
				String key = res.getString("META_KEY");
				String value = res.getString("META_VALUE");				
				ret.setAttribute(key, value);				
			}			
		} catch (SQLException e) {			
			logger.log(Level.SEVERE,"Getting virtual service metadata failed with exeception ->" , e);	
			throw e;
		} finally {			
			release(conn,ps);
		}
		return ret;
	}

	@Override
	public List<DMRegisteredService> getAllVirtualServices() throws SQLException {
		LinkedList<DMRegisteredService> ret  = new LinkedList<DMRegisteredService>();
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			conn = mgr.getConnection();
			String sql = SELECT_ALL_SERVICES.replace("%1", virtualServicesTableName);
		  	ps = conn.prepareStatement(sql);		
			ResultSet res = ps.executeQuery();
			while(res.next()){
				String name = res.getString("NAME");
				String category_name = res.getString("CATEGORY_NAME");
				String endpoint = res.getString("ENDPOINT");
				String esb_proyect = res.getString("ESB_PROYECT");			
				long serviceID = res.getLong("SERVICE_ID");
				
				DMRegisteredService aux = new DMRegisteredService();
				aux.setEndPointURL(endpoint);
				aux.setEsbProyectName(esb_proyect);
				aux.setServiceID(serviceID);
				aux.setVirtualServiceCategory(category_name);
				aux.setVirtualServiceName(name);
				aux.setCanonicalXSL(this.hasCanonicalXSL(serviceID));
				
				//cargo la metadata asociadas
				MetaDataConfig meta = getVirtualServiceMetadata(serviceID);
				aux.setMetadata(meta);
				ret.add(aux);
			}
		} catch (SQLException e) {			
			logger.log(Level.SEVERE,"Getting virtual service failed with exeception ->" , e);		
			throw e;
		} finally {			
			release(conn,ps);
		}		
		return ret;
	}

	@Override
	public int updateService(DMRegisteredService virtualService) throws SQLException {
		int ret = 0;
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			conn = mgr.getConnection();
			String sql = UPDATE_SERVICE.replace("%1", virtualServicesTableName);
		  	ps = conn.prepareStatement(sql);		
		  	ps.setString(1, virtualService.getVirtualServiceName());
			ps.setString(2,virtualService.getVirtualServiceCategory());
			ps.setString(3,virtualService.getEndPointURL());
			ps.setString(4,virtualService.getEsbProyectName());
			ps.setLong(5,virtualService.getServiceID());		  
			ret = ps.executeUpdate();
		} catch (SQLException e) {			
			logger.log(Level.SEVERE,"update virtual service failed with exeception ->" , e);	
			release(conn,ps);
			throw e;
		} finally {			
			release(conn,ps);
		}		
		return ret;
	}

	@Override
	public int updateMetadata(KeyValuePair meta, long serviceID) throws SQLException{
		int ret = 0;
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			conn = mgr.getConnection();
			String sql = UPDATE_METADATA.replace("%1", virtualServicesMetadataTableName);
		  	ps = conn.prepareStatement(sql);		
		  	ps.setString(1, meta.getValue());
		  	ps.setLong(2,serviceID);
		  	ps.setString(3, meta.getKey());		  	
			ret = ps.executeUpdate();
		} catch (SQLException e) {			
			logger.log(Level.SEVERE,"update virtual service metadata failed with exeception ->" , e);	
			release(conn,ps);
			throw e;
		} finally {			
			release(conn,ps);
		}		
		return ret;
	}
		
	@Override
	public LinkedList<DMRegisteredService> getEquivalentVirtualServices(long serviceID)throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * 
	 * @param metaKey key de la metadata a eliminar 
	 * @param serviceID identificador del servicio a cual se le elimina la metadata
	 * Elimina la metadata con la clave dada y registrada para el servicio de identificador ServiceID
	 */
	@Override
	public int deleteVirtualServiceMetadata(String metaKey, long serviceID) throws SQLException {
		int ret = 0;
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			conn = mgr.getConnection();
			String sql = DELETE_METADATA.replace("%1", virtualServicesMetadataTableName);
		  	ps = conn.prepareStatement(sql);		
		  	ps.setString(1, metaKey);
		  	ps.setLong(2,serviceID);
			ret = ps.executeUpdate();
		} catch (SQLException e) {			
			logger.log(Level.SEVERE,"delete virtual service metadata failed with exeception ->" , e);	
			release(conn,ps);
			throw e;
		} finally {			
			release(conn,ps);
		}		
		return ret;
	}

	/**
	 * 
	 * @param serviceID identificador del servicio a eliminar
	 * Elimina el servicio y toda la metadata que este tiene asociado.
	 */
	@Override	
	public int deleteVirtualService(long serviceID) throws SQLException{
		int ret = 0;
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			//Obtengo el servicio
			DMRegisteredService aux = this.getVirtualService(serviceID);
			
			/**borro la metadata*/
			for(KeyValuePair l:aux.getMetadata().attributesAsList()){
				this.deleteVirtualServiceMetadata(l.getKey(), serviceID);
			}
			
			/**borro el servicio */
			conn = mgr.getConnection();
			String sql = DELETE_SERVICE.replace("%1", virtualServicesTableName);
		  	ps = conn.prepareStatement(sql);				  
		  	ps.setLong(1,serviceID);
			ret = ps.executeUpdate();
			
			/**borro los archivo de las transformaciones si tenia*/
			if(this.hasCanonicalXSL(serviceID)){
				this.deleteCanonicalXSL(serviceID);
			}
			
		} catch (SQLException e) {			
			logger.log(Level.SEVERE,"delete virtual service failed with exeception ->" , e);	
			release(conn,ps);
			throw e;
		} finally {			
			release(conn,ps);
		}		
		return ret;
	}

	/**
	 * 
	 * @param conn conexion que se desea cerrar
	 * @param ps preparementStatement que se decea cerrar
	 */
	private void release(Connection conn , PreparedStatement ps) {
		if (ps != null)
			try {
				ps.close();
			} catch (SQLException e) {
				logger.log(Level.SEVERE,"Error when closing prepareStatement ->" , e);
		}			
		
		if (conn != null) {
			try {
				conn.close();
			} catch (Exception e2) {
				logger.log(Level.SEVERE, e2.getMessage(), e2);
			}
		}
	}


	/**
	 * 
	 * @return el proximo id de la secuencia de los servicios
	 */
	private int getNextSeqValue() throws SQLException{
		int ret = 0;
		Connection conn = null;
		PreparedStatement ps = null;
		try {		
			conn = mgr.getConnection();
			String sql =  GET_NEXT_SEQ_VALUE.replace("%1",virtualServicesSeqTableName);
						
			ps = conn.prepareStatement(sql);			
			ResultSet res = ps.executeQuery();
			if(res.next()){
				ret = res.getInt("NEXTSEQ");
			}
		} catch (SQLException e) {			
			logger.log(Level.SEVERE,"getting next seq for virtual service fail with exeception ->" , e);		
			throw e;
		} finally {			
			release(conn,ps);
		}
		return ret;		
	}

	/***
	 *  @param equiv lista de equivalentes para los cuales se agrega la relacion
	 *  @param serviceID identificador del servicio para el cual se desea agregar los equivalentes
	 */
	@Override
	public void persistEquivVirtualServices(List<DMRegisteredService> equivs, long serviceID)throws SQLException {
		Connection conn = null;
		PreparedStatement ps = null;	
		try {
			conn = mgr.getConnection();
			String sql = INSERT_EQUIV.replace("%1", virtualServicesEquivTableName);		
			ps = conn.prepareStatement(sql);		
			for(DMRegisteredService eq : equivs){				
				ps.clearParameters();					
				ps.setLong(1,serviceID);
				ps.setLong(2,eq.getServiceID());
				ps.execute();			
			}			
			
		} catch (SQLException e) {			
			logger.log(Level.SEVERE,"Persisting virtual service equivalents failed with exeception ->" , e);
			throw e;
			
		} finally {			
			release(conn,ps);
		}
	}

	@Override
	public LinkedList<DMRegisteredService> getAllEquivVirtualServices(long serviceID) throws SQLException {	    
		LinkedList<DMRegisteredService> ret  = new LinkedList<DMRegisteredService>();		
		Connection conn = null;
		PreparedStatement ps = null;		
		try {
			conn = mgr.getConnection();
			String sql = SELECT_EQUIV_SERVICE.replace("%1", virtualServicesEquivTableName);
		  	ps = conn.prepareStatement(sql);		
		  	ps.setLong(1, serviceID);
			ResultSet res = ps.executeQuery();
			while(res.next()){
				long equiv_id= res.getLong("EQUIV_ID");								
				DMRegisteredService aux = this.getVirtualService(equiv_id);
				ret.add(aux);
			}
		} catch (SQLException e) {			
			logger.log(Level.SEVERE,"Getting virtual service equiv failed with exeception ->" , e);		
			throw e;
		} finally {			
			release(conn,ps);
		}		
		return ret;
	}

	@Override
	public int deleteEquivRelation(long serviceID) throws SQLException {
		int ret = 0;
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			
			conn = mgr.getConnection();
			String sql = DELETE_EQUIV_ALL.replace("%1", virtualServicesEquivTableName);
		  	ps = conn.prepareStatement(sql);				  
		  	ps.setLong(1,serviceID);
			ret = ps.executeUpdate();
			
		} catch (SQLException e) {			
			logger.log(Level.SEVERE,"delete virtual service equiv failed with exeception ->" , e);	
			release(conn,ps);
			throw e;
		} finally {			
			release(conn,ps);
		}		
		return ret;
	}

	@Override
	public void saveToCanonicalXSL(String xsl, Long serviceID) throws IOException {	
		String path = makeFilePath(serviceID, DEFAULT_TO_CANONICAL_SUFFIX);
		File f = new File(path);		
		FileUtils.write(f, xsl);
	}

	@Override
	public void saveFromCanonicalXSL(String xsl, Long serviceID) throws IOException{	
		String path = makeFilePath(serviceID, DEFAULT_FROM_CANONICAL_SUFFIX);
		File f = new File(path);
		FileUtils.write(f, xsl);
	}

	@Override
	public String getToCanonicalXSL(Long serviceID) throws IOException {	
		String path = makeFilePath(serviceID, DEFAULT_TO_CANONICAL_SUFFIX);
		return readFile(path);
	}

	@Override
	public String getFromCanonicalXSL(Long serviceID) throws IOException {
		String path = makeFilePath(serviceID, DEFAULT_FROM_CANONICAL_SUFFIX);
		return readFile(path);
	}
	
	@Override
	public Boolean hasCanonicalXSL(Long serviceID){
		Boolean ret = false;
		try{
			String path1 = makeFilePath(serviceID, DEFAULT_FROM_CANONICAL_SUFFIX);
			String path2 = makeFilePath(serviceID, DEFAULT_TO_CANONICAL_SUFFIX);
			File f1 = new File(path1);
			File f2 = new File(path2);
			ret = f1.exists() && f2.exists();
		}catch(Exception ex){
			logger.log(Level.SEVERE,"Checking canonical XSL for virtual service failed with exeception ->" , ex);	
		}		
		return ret;
	}

	private void deleteCanonicalXSL(long serviceID) {
		try{
			String path1 = makeFilePath(serviceID, DEFAULT_FROM_CANONICAL_SUFFIX);
			String path2 = makeFilePath(serviceID, DEFAULT_TO_CANONICAL_SUFFIX);
			File f1 = new File(path1);
			File f2 = new File(path2);
			f1.delete();
			f2.delete();
		}catch(Exception ex){
			logger.log(Level.SEVERE,"Deleting canonical XSL for virtual service failed with exeception ->" , ex);	
		}		
	}
	
	private String readFile(String path) throws IOException{
		File f = new File(path);
		String ret = "";
		if(f.canRead()){
			ret = FileUtils.readFileToString(f);
		}else{
			logger.warning("Imposible to READ the xls file.");
		}
		return ret;
	}
			
	private String makeFilePath(Long serviceID , String suffix){
		StringBuilder sb = new StringBuilder();
		sb.append(xslFolder);
		sb.append("/");
		sb.append(serviceID.toString());
		sb.append("_");
		sb.append(suffix);
		return sb.toString();
	}
	
}
